
import java.lang.reflect.Method;
import java.util.Vector;
import javax.swing.JOptionPane;
import org.antlr.runtime.Token;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Infinity
 */
public class ParserProxy implements ProgrammingLanguageParser
{

    String language;
    Vector<Token> tree;
    Vector<Token> colorTree;
    
    ParserProxy(String l)
    {
        language = l;
        tree = new Vector<Token>();
        colorTree = new Vector<Token>();
    }
    
    @Override
    public void parse(String filename) 
    {
        try 
        {
            PetersClassLoader myLoader = new PetersClassLoader();

            Class myClass = myLoader.loadClass(language);

            LanguageParser whatInstance = (LanguageParser) myClass.newInstance();
            //String methodParameter = "a quick brown fox";

            Method myMethod = myClass.getMethod("parse", new Class[] { String.class });
            //Method myMethod = myClass.getMethod("parse", new Class[]{}); 

            Object returnValue = myMethod.invoke(whatInstance, new Object[] { filename });
            //Object returnValue = myMethod.invoke(whatInstance, null);
        } 
        catch (Exception ex) 
        {
            JOptionPane.showMessageDialog(null, ex.toString(), "Parse Error", JOptionPane.ERROR_MESSAGE);
        }
    }
    
    @Override
    public void parseString(String filename) 
    {
        try 
        {
            PetersClassLoader myLoader = new PetersClassLoader();

            Class myClass = myLoader.loadClass(language);

            LanguageParser whatInstance = (LanguageParser) myClass.newInstance();
            //String methodParameter = "a quick brown fox";

            Method myMethod = myClass.getMethod("parseString", new Class[] { String.class });
            //Method myMethod = myClass.getMethod("parse", new Class[]{}); 

            Object returnValue = myMethod.invoke(whatInstance, new Object[] { filename });
            //Object returnValue = myMethod.invoke(whatInstance, null);
            //System.out.println(returnValue);
        } 
        catch (Exception ex) 
        {
            JOptionPane.showMessageDialog(null, ex.toString(), "Parse Error", JOptionPane.ERROR_MESSAGE);
        }
    }

    @Override
    public Vector<Token> getTree() 
    {
        try 
        {
            PetersClassLoader myLoader = new PetersClassLoader();

            Class myClass = myLoader.loadClass(language);

            LanguageParser whatInstance = (LanguageParser) myClass.newInstance();

            Method myMethod = myClass.getMethod("getTree", new Class[]{}); 

            Vector<Token> returnValue = (Vector<Token>) myMethod.invoke(whatInstance, null);
            tree = returnValue;
        } 
        catch (Exception ex) 
        {
            JOptionPane.showMessageDialog(null, ex.toString(), "Parse Error", JOptionPane.ERROR_MESSAGE);
        }
        return tree;
    }

    @Override
    public Vector<Token> parseColor(String filename) 
    {
        Vector<Token> colorList = new Vector<Token>();
        try 
        {
            PetersClassLoader myLoader = new PetersClassLoader();

            Class myClass = myLoader.loadClass(language);

            LanguageParser whatInstance = (LanguageParser) myClass.newInstance();

            Method myMethod = myClass.getMethod("parseColor", new Class[] { String.class });

            colorList = (Vector<Token>) myMethod.invoke(whatInstance, new Object[] { filename });
        } 
        catch (Exception ex) 
        {
            JOptionPane.showMessageDialog(null, ex.toString(), "Parse Error", JOptionPane.ERROR_MESSAGE);
        }
        return colorList;
    }
}
